package net.wenku8.reader.view.file

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.ui.lite.LiteTitle
import net.wenku8.reader.ui.lite.largePadding
import net.wenku8.reader.ui.lite.mediumPadding
import net.wenku8.reader.ui.lite.onSwipe
import net.wenku8.reader.ui.lite.onTap
import net.wenku8.reader.ui.lite.smallPadding
import net.wenku8.reader.ui.theme.LiteGray
import net.wenku8.reader.view.common.ReadBgImage
import net.wenku8.reader.view.common.ReadBottom
import net.wenku8.reader.view.common.ReadSettingModel

@Composable
fun FileRead() {
    val fileReadViewModel = viewModel<FileReadViewModel>()
    val readSettingModel = viewModel<ReadSettingModel>()
    readSettingModel.initData()
    LaunchedEffect(Unit) {
        fileReadViewModel.initData()
    }
    Box(
        modifier = Modifier.statusBarsPadding().fillMaxSize()
            .background(readSettingModel.bgColor)
    ) {
        ReadBgImage()
        Column(
            modifier = Modifier
                .largePadding()
                .background(Color.Transparent)
                .onSwipe { fileReadViewModel.changeChapter(it) }
                .onTap(onClick = {
                    readSettingModel.show = true
                })
        ) {
            Box(
                modifier = Modifier.fillMaxWidth().smallPadding().background(LiteGray)
            ) {
                Column(
                    modifier = Modifier.mediumPadding()
                ) {
                    LiteTitle(text = fileReadViewModel.chapter.title, color = readSettingModel.textColor)
                }
            }
            if (fileReadViewModel.loaded){
                LazyColumn (
                    modifier = Modifier.fillMaxWidth()
                ){
                    items(items = fileReadViewModel.textList) { text ->
                        Row(
                            modifier = Modifier.padding(top = readSettingModel.spaceSize)
                        ) {
                            Text(text = text, color = readSettingModel.textColor, fontSize = readSettingModel.fontSize)
                        }
                    }
                }
            }
        }
    }
    ReadBottom()
}
